
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.utils.dateparse &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-dateparse">
            
  <h1>Source code for django.utils.dateparse</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Functions to parse datetime objects.&quot;&quot;&quot;</span>

<span class="c1"># We&#39;re using regular expressions rather than time.strptime because:</span>
<span class="c1"># - They provide both validation and parsing.</span>
<span class="c1"># - They&#39;re more flexible for datetimes.</span>
<span class="c1"># - The date/datetime/time constructors produce friendlier error messages.</span>

<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.timezone</span> <span class="k">import</span> <span class="n">get_fixed_timezone</span><span class="p">,</span> <span class="n">utc</span>

<span class="n">date_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;(?P&lt;year&gt;\d</span><span class="si">{4}</span><span class="s1">)-(?P&lt;month&gt;\d{1,2})-(?P&lt;day&gt;\d{1,2})$&#39;</span>
<span class="p">)</span>

<span class="n">time_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;(?P&lt;hour&gt;\d{1,2}):(?P&lt;minute&gt;\d{1,2})&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?::(?P&lt;second&gt;\d{1,2})(?:\.(?P&lt;microsecond&gt;\d{1,6})\d{0,6})?)?&#39;</span>
<span class="p">)</span>

<span class="n">datetime_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;(?P&lt;year&gt;\d</span><span class="si">{4}</span><span class="s1">)-(?P&lt;month&gt;\d{1,2})-(?P&lt;day&gt;\d{1,2})&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;[T ](?P&lt;hour&gt;\d{1,2}):(?P&lt;minute&gt;\d{1,2})&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?::(?P&lt;second&gt;\d{1,2})(?:\.(?P&lt;microsecond&gt;\d{1,6})\d{0,6})?)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?P&lt;tzinfo&gt;Z|[+-]\d</span><span class="si">{2}</span><span class="s1">(?::?\d</span><span class="si">{2}</span><span class="s1">)?)?$&#39;</span>
<span class="p">)</span>

<span class="n">standard_duration_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;^&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:(?P&lt;days&gt;-?\d+) (days?, )?)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;((?:(?P&lt;hours&gt;-?\d+):)(?=\d+:\d+))?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:(?P&lt;minutes&gt;-?\d+):)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?P&lt;seconds&gt;-?\d+)&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:\.(?P&lt;microseconds&gt;\d{1,6})\d{0,6})?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;$&#39;</span>
<span class="p">)</span>

<span class="c1"># Support the sections of ISO 8601 date representation that are accepted by</span>
<span class="c1"># timedelta</span>
<span class="n">iso8601_duration_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
    <span class="sa">r</span><span class="s1">&#39;^(?P&lt;sign&gt;[-+]?)&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;P&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:(?P&lt;days&gt;\d+(.\d+)?)D)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:T&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:(?P&lt;hours&gt;\d+(.\d+)?)H)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:(?P&lt;minutes&gt;\d+(.\d+)?)M)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;(?:(?P&lt;seconds&gt;\d+(.\d+)?)S)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;)?&#39;</span>
    <span class="sa">r</span><span class="s1">&#39;$&#39;</span>
<span class="p">)</span>


<div class="viewcode-block" id="parse_date"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.dateparse.parse_date">[docs]</a><span class="k">def</span> <span class="nf">parse_date</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Parses a string and return a datetime.date.</span>

<span class="sd">    Raises ValueError if the input is well formatted but not a valid date.</span>
<span class="sd">    Returns None if the input isn&#39;t well formatted.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">match</span> <span class="o">=</span> <span class="n">date_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="n">match</span><span class="o">.</span><span class="n">groupdict</span><span class="p">())}</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">)</span></div>


<div class="viewcode-block" id="parse_time"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.dateparse.parse_time">[docs]</a><span class="k">def</span> <span class="nf">parse_time</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Parses a string and return a datetime.time.</span>

<span class="sd">    This function doesn&#39;t support time zone offsets.</span>

<span class="sd">    Raises ValueError if the input is well formatted but not a valid time.</span>
<span class="sd">    Returns None if the input isn&#39;t well formatted, in particular if it</span>
<span class="sd">    contains an offset.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">match</span> <span class="o">=</span> <span class="n">time_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">groupdict</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microsecond&#39;</span><span class="p">]:</span>
            <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microsecond&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microsecond&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s1">&#39;0&#39;</span><span class="p">)</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="n">kw</span><span class="p">)</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">}</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">time</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">)</span></div>


<div class="viewcode-block" id="parse_datetime"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.dateparse.parse_datetime">[docs]</a><span class="k">def</span> <span class="nf">parse_datetime</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Parses a string and return a datetime.datetime.</span>

<span class="sd">    This function supports time zone offsets. When the input contains one,</span>
<span class="sd">    the output uses a timezone with a fixed offset from UTC.</span>

<span class="sd">    Raises ValueError if the input is well formatted but not a valid datetime.</span>
<span class="sd">    Returns None if the input isn&#39;t well formatted.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">match</span> <span class="o">=</span> <span class="n">datetime_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">groupdict</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microsecond&#39;</span><span class="p">]:</span>
            <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microsecond&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microsecond&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s1">&#39;0&#39;</span><span class="p">)</span>
        <span class="n">tzinfo</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;tzinfo&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">tzinfo</span> <span class="o">==</span> <span class="s1">&#39;Z&#39;</span><span class="p">:</span>
            <span class="n">tzinfo</span> <span class="o">=</span> <span class="n">utc</span>
        <span class="k">elif</span> <span class="n">tzinfo</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">offset_mins</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:])</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">3</span> <span class="k">else</span> <span class="mi">0</span>
            <span class="n">offset</span> <span class="o">=</span> <span class="mi">60</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">])</span> <span class="o">+</span> <span class="n">offset_mins</span>
            <span class="k">if</span> <span class="n">tzinfo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;-&#39;</span><span class="p">:</span>
                <span class="n">offset</span> <span class="o">=</span> <span class="o">-</span><span class="n">offset</span>
            <span class="n">tzinfo</span> <span class="o">=</span> <span class="n">get_fixed_timezone</span><span class="p">(</span><span class="n">offset</span><span class="p">)</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="n">kw</span><span class="p">)</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">}</span>
        <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;tzinfo&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">tzinfo</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">)</span></div>


<div class="viewcode-block" id="parse_duration"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.dateparse.parse_duration">[docs]</a><span class="k">def</span> <span class="nf">parse_duration</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Parses a duration string and returns a datetime.timedelta.</span>

<span class="sd">    The preferred format for durations in Django is &#39;%d %H:%M:%S.%f&#39;.</span>

<span class="sd">    Also supports ISO 8601 representation.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">match</span> <span class="o">=</span> <span class="n">standard_duration_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">match</span><span class="p">:</span>
        <span class="n">match</span> <span class="o">=</span> <span class="n">iso8601_duration_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">groupdict</span><span class="p">()</span>
        <span class="n">sign</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;sign&#39;</span><span class="p">,</span> <span class="s1">&#39;+&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;-&#39;</span> <span class="k">else</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;microseconds&#39;</span><span class="p">):</span>
            <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microseconds&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microseconds&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s1">&#39;0&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;seconds&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;microseconds&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;seconds&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;-&#39;</span><span class="p">):</span>
            <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microseconds&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;-&#39;</span> <span class="o">+</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;microseconds&#39;</span><span class="p">]</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="n">kw</span><span class="p">)</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">}</span>
        <span class="k">return</span> <span class="n">sign</span> <span class="o">*</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>